package com.sptpc.qimoxiangmu.controller;


import cn.hutool.json.JSONObject;
import com.sptpc.qimoxiangmu.controller.request.UserRequest;
import com.sptpc.qimoxiangmu.model.Type;
import com.sptpc.qimoxiangmu.model.User;
import com.sptpc.qimoxiangmu.model.vo.UserVo;
import com.sptpc.qimoxiangmu.service.UserService;
import com.sptpc.qimoxiangmu.util.ServerResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("/insert.html")
    public ModelAndView insertView() {
        ModelAndView mv = new ModelAndView("user/insert");
        mv.addObject("userList", userService.findAll());
        return mv;
    }

    @GetMapping("/edit.html")
    public ModelAndView redirct_update(@RequestParam("id") Long id) {
        ModelAndView mv = new ModelAndView("user/edit");
        User user = userService.findOne(id);//通过id查询名字
        mv.addObject("id", id);
        mv.addObject("user", user);
        return mv;
    }

    @GetMapping("/list.html")
    public ModelAndView listView() {
        ModelAndView mv = new ModelAndView("user/list");
        List<User> userList = userService.findAll();
        mv.addObject("userList", userList);
        return mv;
    }

    //user的插入
    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)//表单形式，x.www.urlencoded，成功
    @ResponseBody
    public ServerResponse getUser(@RequestParam String user,
                                  @RequestParam String password,
                                  @RequestParam String info
    ) {
        return ServerResponse.createBySuccess(userService.saveUser(user, password, info));
    }

    //user的更新
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public ServerResponse update(UserVo userVo) {
        return ServerResponse.createBySuccess(userService.update(userVo));
    }

    //user表整体查询
    @GetMapping("/")//获取全部数据，成功
    @ResponseBody
    public ServerResponse list() {
        return ServerResponse.createBySuccess(userService.findAll());
    }

    //    删除
    @GetMapping("/delete")//成功
    @ResponseBody
    public ServerResponse delete(@RequestParam(name = "id") Long id) {
        User user = userService.findOne(id);
        if (user != null) {
            userService.deleteUser(id);
            return ServerResponse.createBySuccessMessage("删除成功！");
        } else {
            return ServerResponse.createByErrorMessage("找不到删除分类！");
        }
    }

    //名字查询
    @GetMapping(value = "/", params = "user")
    @ResponseBody
    public ServerResponse findOne(@RequestParam String user) {
        return ServerResponse.createBySuccess(userService.findOne(user));
    }

    //通过id查询
    @GetMapping(value = "/{id}")
    @ResponseBody
    public ServerResponse findOne(@PathVariable Long id) {
        return ServerResponse.createBySuccess(userService.findOne(id));
    }

    @PostMapping("login")
    @ResponseBody
    public JSONObject login(
            @RequestParam String username,
            @RequestParam String password
    ){
        return userService.login(username, password);
    }


}